//
//  DetailsViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2018/11/9.
//  Copyright © 2018 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire
class DetailsViewController: UIViewController {
     var tableView: UITableView!
    var DetailsArray = [DetailsModel]()
    var numberString:String!
    var nameArray : [String] = ["企业名称","详细地址","董事长名称","电话号码","总经理名字","电话号码","邮箱","传真","邮编","职工人数","经营范围","投资外方","投资总额","投资方比例","年产值","年销售额"]
    
    var NRArray = [String]()

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
         self.loadData()
        
     
        self.view.backgroundColor = bgGrayColor;
        navigationItem.title="详情"
       
        
       
    }
    
    func getTableview(){
        tableView = UITableView(frame:self.view.frame, style:.plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        
        tableView.tableFooterView = UIView.init()
        
        
        tableView.register(NSClassFromString("DetailsViewTableViewCell"), forCellReuseIdentifier: "DetailsViewTableViewCell")
        
        
        view.addSubview(tableView)
        tableView.sd_layout()?.spaceToSuperView(UIEdgeInsets.zero)
    }
    
    func loadData()  {
        

        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/companyinformation/detail"
        
        let json = "{\"id\":\"\(self.numberString!)\"}"
        
        //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
        
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
         
            if response.result.isSuccess {
                
                
                let json = JSON(response.data!)
           
                
                let compayName = json["resultData"]["compayName"].string
                
                let address = json["resultData"]["address"].string
                
                let ePerson = json["resultData"]["ePerson"].string
                
                let ePTel = json["resultData"]["ePTel"].string
                
                
                let contactPerson = json["resultData"]["contactPerson"].string
                
                let cPTel = json["resultData"]["cPTel"].string
                
                let email = json["resultData"]["email"].string
                
                let fax = json["resultData"]["fax"].string
                
                let postCode = json["resultData"]["postCode"].string
                
                let workerNum = json["resultData"]["workerNum"].string

                
                let scopeOperation = json["resultData"]["scopeOperation"].string

                
                let foreignInvestment = json["resultData"]["foreignInvestment"].string

                
                let investmentTotal = json["resultData"]["investmentTotal"].string

                let investmentProportion = json["resultData"]["investmentProportion"].string
                
                let annualOutput = json["resultData"]["annualOutput"].string
                
                let annualSales = json["resultData"]["annualSales"].string
                
                self.NRArray = [compayName!,address!,ePerson!,ePTel!,contactPerson!,cPTel!,email!,fax!,postCode!,workerNum!,scopeOperation!,foreignInvestment!,investmentTotal!,investmentProportion!,annualOutput!,annualSales!]
       
       
                   self.getTableview()
//                var NRArray : [String] = []
                
                
            
            }
        
        
        
        }
    }
    

}

extension DetailsViewController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return nameArray.count
        
    }
    
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 80
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = DetailsViewTableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "DetailsViewTableViewCell")
        cell.backgroundColor = UIColor.groupTableViewBackground
        cell.nameLabel.text = nameArray[indexPath.row]
        
        cell.DetailsLabel.text = NRArray[indexPath.row]

        
        return cell
    }

}
